package com.association.model;

import com.association.constant.CodeEnum;
import com.association.utils.MessagesManager;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.Map;

/**
 * Created by keyong on 2020/4/20/020.
 */
@Data
@ApiModel(value = "resultResp",description = "返回数据")
public class ResultResp<T> {

    // region // code
    @ApiModelProperty(name = "code",value = "状态码", required = true, example = "200")
    // endregion
    protected String code = CodeEnum.SUCCESS.getCode();
    // region // message
    @ApiModelProperty(name = "message",value = "返回消息", required = true, example = "成功")
    // endregion
    protected String message = CodeEnum.SUCCESS.getMessage();
    // region // data
    @ApiModelProperty(name = "data",value = "结果", hidden = true)
    // endregion
    protected T data;
    // region // list
    @ApiModelProperty(name = "list",value = "列表", hidden = true)
    // endregion
    protected List<T> list;
    // region // extMap
    @ApiModelProperty(name = "extMap",value = "扩展集", hidden = true)
    // endregion
    protected Map<String, Object> extMap;

    @ApiModelProperty(name = "count",value = "总条数", hidden = true)
    // endregion
    protected Integer count;

    public ResultResp() {
    }

    public ResultResp(T data) {
        this.data = data;
    }

    public ResultResp(List<T> list) {
        this.list = list;
    }

    public ResultResp(List<T> list,Integer count) {
        this.count = count;
        this.list = list;
    }

    public ResultResp(CodeEnum codeEnum) {
        this.code = codeEnum.getCode();
        String message = MessagesManager.getMessage(Integer.parseInt(codeEnum.getCode()));
        if (StringUtils.isBlank(message) || "\"\"".equals(message)) message = codeEnum.getMessage();
        this.message = message;
    }

    public ResultResp(String code, String message) {
        this.code = code;
        this.message = message;
    }

    public ResultResp(CodeEnum codeEnum, String message) {
        this(codeEnum.getCode(), message);
    }
}
